package com.ys.nms.module.business.service.sensors;

import cn.hutool.core.collection.CollUtil;
import com.ys.nms.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ys.nms.module.business.controller.admin.sensors.vo.*;
import com.ys.nms.module.business.dal.dataobject.sensors.SensorsDO;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.util.object.BeanUtils;

import com.ys.nms.module.business.dal.mysql.sensors.SensorsMapper;

import static com.ys.nms.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.convertList;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.diffList;
import static com.ys.nms.module.business.enums.ErrorCodeConstants.*;

/**
 * 传感器 Service 实现类
 *
 * @author zzy
 */
@Service
@Validated
public class SensorsServiceImpl implements SensorsService {

    @Resource
    private SensorsMapper sensorsMapper;

    @Override
    public Integer createSensors(SensorsSaveReqVO createReqVO) {
        // 插入
        SensorsDO sensors = BeanUtils.toBean(createReqVO, SensorsDO.class);
        sensorsMapper.insert(sensors);

        // 返回
        return sensors.getSensorId();
    }

    @Override
    public void updateSensors(SensorsSaveReqVO updateReqVO) {
        // 校验存在
        validateSensorsExists(updateReqVO.getSensorId());
        // 更新
        SensorsDO updateObj = BeanUtils.toBean(updateReqVO, SensorsDO.class);
        sensorsMapper.updateById(updateObj);
    }

    @Override
    public void deleteSensors(Integer id) {
        // 校验存在
        validateSensorsExists(id);
        // 删除
        sensorsMapper.deleteById(id);
    }

    @Override
        public void deleteSensorsListByIds(List<Integer> ids) {
        // 删除
        sensorsMapper.deleteByIds(ids);
        }


    private void validateSensorsExists(Integer id) {
        if (sensorsMapper.selectById(id) == null) {
            throw exception(SENSORS_NOT_EXISTS);
        }
    }

    @Override
    public SensorsDO getSensors(Integer id) {
        return sensorsMapper.selectById(id);
    }

    @Override
    public PageResult<SensorsDO> getSensorsPage(SensorsPageReqVO pageReqVO) {
        return sensorsMapper.selectPage(pageReqVO);
    }

    @Override
    public SensorsDO getCurrent(SensorsPageReqVO pageReqVO) {
        LambdaQueryWrapperX<SensorsDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(SensorsDO::getDeviceId, pageReqVO.getDeviceId());
        wrapperX.eq(SensorsDO::getSensorDeleted, 0);
        wrapperX.eqIfPresent(SensorsDO::getDeviceId, pageReqVO.getDeviceId());
        wrapperX.eqIfPresent(SensorsDO::getSensorClass, pageReqVO.getSensorClass());
        wrapperX.eqIfPresent(SensorsDO::getSensorOid, pageReqVO.getSensorOid());
        wrapperX.orderByDesc(SensorsDO::getLastupdate);
        return sensorsMapper.selectList().getFirst();

    }

}